using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;

namespace Demo
{
    public class School
    {
        public string schoolName;
        public int schoolID;
        public List<int> studentIDs;

        public void WriteSchoolDes()
        {
            Debug.Log($"SchoolName:{schoolName}");
            Debug.Log($"SchoolID:{schoolID}");
            for (int i = 0; i < studentIDs.Count; i++)
            {
                Debug.Log($"Student{i}:{studentIDs[i]}");
            }
        }
    }
    
    public struct StudentData
    {
        public string studentName;
        public int studentID;
        public List<string> courses;

        public void WriteSchoolDes()
        {
            Debug.Log($"StudentName:{studentName}");
            Debug.Log($"studentID:{studentID}");
            for (int i = 0; i < courses.Count; i++)
            {
                Debug.Log($"Course{i}:{courses[i]}");
            }
        }
    }

    public ref struct RefStruct
    {
        public int phoneNumber;

        public string Test;
    }

    public class RefExample:MonoBehaviour
    {
        //值类型需要借助ref可以修改值
        private int score = 0;
        //引用类型本身就可以传递
        private School school;
        //值类型在通过ref形参传递时 才会影响自身的值
        private StudentData studentData;

        private int numberPhone = 000000;
        void Start()
        {
            school = new School();
            school.schoolName = "AA";
            school.schoolID = 111111;
            school.studentIDs = new List<int>();
            school.studentIDs.Add(10001);
                    
            studentData = new StudentData();
            studentData.studentID = 10001;
            studentData.studentName = "BB";
            studentData.courses = new List<string>();
            studentData.courses.Add("C++");
        }

        #region 值传递与Ref值传递测试测试

        [Button("基本引用传递测试")]
        void SimapleTest()
        {
            Debug.Log("==========RefExample=========");
            
            Debug.Log("原始值:"+score);
            
            ChangeValue(score);
            Debug.Log("形参传递后:"+score);
            
            ChangeValue(ref score);
            Debug.Log("Ref修饰形参后:"+score);
            
            Debug.Log("==========RefExample==========");
        }
        void ChangeValue(int value)
        {
            value = 10;
        }

        void ChangeValue(ref int value)
        {
            value = 20;
        }

        #endregion
        
        #region 引用传递测试

        [Button("基于类的引用传递测试")]
        void ClassTest()
        {
            Debug.Log("===基本传递===");
            ChangeSchoolData(school);
            school.WriteSchoolDes();
            
            Debug.Log("===Ref引用传递===");
            ChangeSchoolData(ref school);
            school.WriteSchoolDes();
            
            //结论两者一样 school本身就是引用类型
        }
        void ChangeSchoolData(School school)
        {
            school.schoolID = 222222;
            school.studentIDs.Add(10002);
            school.schoolName = "BBB";
        }
        void ChangeSchoolData(ref School school)
        {
            school.schoolID = 333333;
            school.studentIDs.Add(10003);
            school.schoolName = "CCC";
        }

        #endregion

        #region 结构体引用传递

        private void StructTest()
        {
            studentData.WriteSchoolDes();
            ChangeSutdentData(studentData);
            studentData.WriteSchoolDes();
            ChangeSutdentData(ref studentData);
            studentData.WriteSchoolDes();
        }
        
        void ChangeSutdentData(StudentData studentData)
        {
            studentData.studentName = "StructStudent";
            studentData.studentID = 10002;
            studentData.courses.Add("Java");
          
        }
        void ChangeSutdentData(ref StudentData studentData)
        {
            studentData.studentName = "RefStructStudent";
            studentData.studentID = 10003;
            studentData.courses.Add("C#");
           
        }

        #endregion
        

        public int intValue_A = 0;
        public ref int ToRoundInt(float value)
        {
            intValue_A = Mathf.RoundToInt(value);
            return ref intValue_A;
        }
    }
}